using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input.Touch;

namespace Algorhythm
{
    public enum GameState : int
    {
        None = 0,
        Start,
        Menu,
        Game,
        Score,
    }

    class ScreenManager
    {
        #region property variable
        Game1 game;
        List<Screen> listScreen = new List<Screen>();
        Screen currentScreen;
        GameState gameState;
        float elaspedTime;
        bool loadResult;
        #endregion

        public bool LoadResult
        {
            get
            {
                return loadResult;
            }

            set
            {
                loadResult = value;
            }
        }

        public void Initialize(Game1 game)
        {
            this.game = game;
            gameState = GameState.Start;
            elaspedTime = 0.0f;
            loadResult = false;

            // create all screen
            TitleScreen titleScreen = new TitleScreen();
            titleScreen.Initialize(game, this, "Title");
            AddScreen(titleScreen);

            MenuScreen menuScreen = new MenuScreen();
            menuScreen.Initialize(game, this, "Menu");
            AddScreen(menuScreen);

            GameScreen gameScreen = new GameScreen();
            gameScreen.Initialize(game, this, "Game");
            AddScreen(gameScreen);

            // set first screen
            ChanageState(GameState.Start);
        }

        public void LoadContent()
        {
            foreach (Screen screen in listScreen)
            {
                screen.LoadContent();
            }

            loadResult = true;
        }

        public void UnLoadContent()
        {
            foreach (Screen screen in listScreen)
            {
                screen.UnloadContent();
            }
        }

        public void AddScreen(Screen screen)
        {
            listScreen.Add(screen);
        }

        public void ChanageState(GameState gs)
        {
            gameState = gs;

            switch (gameState)
            {
                case GameState.None:
                    //ChanageState("None");
                    break;
                case GameState.Start:
                    ChangeScreen("Title");
                    break;
                case GameState.Menu:
                    ChangeScreen("Menu");
                    break;
                case GameState.Game:
                    ChangeScreen("Game");
                    break;
                case GameState.Score:
                    ChangeScreen("Score");
                    break;
                default:
                    break;
            }
        }

        public void ChangeScreen(string screenName)
        {
            foreach (Screen screen in listScreen)
            {
                if (screen.Name == screenName)
                {
                    currentScreen = screen;
                }
            }
        }

        public void Update(GameTime gameTime)
        {
            currentScreen.Update(gameTime);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            currentScreen.Draw(spriteBatch);
        }

        public void StartMusic(int index)
        {
            foreach (Screen screen in listScreen)
            {
                if (screen.Name == "Game")
                {
                    GameScreen gs = (GameScreen)screen;
                    gs.StartMusicIndex = index;
                }
            }
        }
    }
}
